<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="手机号" v-if="handleCheckPermi('system:dept:logo') || handleCheckRole('admin')" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入真实姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证" prop="identityNumber">
        <el-input v-model="queryParams.identityNumber" placeholder="请输入身份证" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="客户端" prop="clientId" v-if="handleCheckRole('admin')">
        <el-select v-model="queryParams.clientId" placeholder="请选择客户端" clearable size="small">
          <el-option v-for="item in clientList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wx:user:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['wx:user:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-s-custom" size="mini" :disabled="single" @click="findFriends"
          v-hasPermi="['wx:user:findFriends']">查询好友</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-money" size="mini" :disabled="single" @click="balanceMx"
          v-hasPermi="['wx:user:balanceMx']">余额明细</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-coin" size="mini" :disabled="single" @click="integralMx"
          v-hasPermi="['wx:user:integralMx']">积分明细</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-coin" size="mini" :disabled="single" @click="userAddress"
          v-hasPermi="['wx:user:query']">收货地址</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-s-custom" size="mini" :disabled="single" @click="checkDetail"
          v-hasPermi="['wx:user:query']">详细信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-s-custom" size="mini" :disabled="single" @click="checkMember"
          v-hasPermi="['wx:require:query']">会员信息</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-s-custom" size="mini" :disabled="single" @click="checkAuth"
          v-hasPermi="['wx:auth:query']">认证资料</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-s-custom" size="mini" :disabled="single"
          v-hasPermi="['ypp:user:query']">
          <router-link :to="{ path: '/zx/data/user', query: { id: ids[0] }, params: { to: 1 } }">
            使用人列表
          </router-link>
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="100" />
      <el-table-column label="头像" align="center" prop="headUrl"  width="120">
        <template slot-scope="scope">
          <el-image :src="scope.row.headUrl" style="height: 40px;width: 40px" />
        </template>
      </el-table-column>
      <el-table-column label="真实姓名" align="center" prop="realName"  width="120" />
      <el-table-column label="昵称" align="center" prop="nickName" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="手机号" align="center" prop="phone"  width="140" />
      <el-table-column label="身份号" align="center" prop="identityNumber" width="160" />
      <!-- <el-table-column label="唯一标识" align="center" prop="openid" width="120" :show-overflow-tooltip="true" /> -->
      <el-table-column label="状态" align="center" :formatter="statusFormat" />
      <el-table-column label="客户端" align="center" v-if="handleCheckRole('admin')" :show-overflow-tooltip="true"
        :formatter="clientFormat" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改微信用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-table-column label="ID" align="center" prop="id" />
        <el-table-column label="昵称" align="center" prop="nickName" />
        <el-table-column label="头像" align="center" prop="headUrl">
          <template slot-scope="scope">
            <el-image :src="scope.row.headUrl" />
          </template>
        </el-table-column>
        <el-table-column label="唯一标识" align="center" prop="openid" width="320" />
        <el-table-column label="手机号" align="center" prop="phone" />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="微信好友" :visible.sync="visible" ref="queryForm2" width="1000px" top="5vh" append-to-body>
      <el-form :model="queryParams2" :inline="true" label-width="68px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="queryParams2.nickName" placeholder="请输入昵称" clearable size="small" />
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="queryParams2.phone" placeholder="请输入手机号" clearable size="small" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery2">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery2">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-table :data="dbTableList" height="260px">
          <el-table-column label="ID" align="center" prop="id" />
          <el-table-column label="昵称" align="center" prop="nickName" />
          <el-table-column label="头像" align="center" prop="headUrl">
            <template slot-scope="scope">
              <el-image :src="scope.row.headUrl" style="height: 40px;width: 40px" />
            </template>
          </el-table-column>
          <el-table-column label="唯一标识" align="center" prop="openid" width="320" />
          <el-table-column label="手机号" align="center" prop="phone" />

          <el-table-column label="真实姓名" align="center" prop="realName" />
          <el-table-column label="性别" align="center" :formatter="genderFormat" />
          <el-table-column label="状态" align="center" :formatter="statusFormat" />
        </el-table>
        <pagination v-show="total2 > 0" :total="total2" :page.sync="queryParams2.pageNum"
          @pagination="findFriends(undefined)" />
      </el-row>
    </el-dialog>

    <el-dialog title="用户余额明细" :visible.sync="visible2" width="1000px" top="5vh" append-to-body>
      <el-row>
        <el-table :data="mxTableList" height="260px">
          <el-table-column label="ID" align="center" prop="id" />
          <el-table-column label="金额" align="center" prop="money" />
          <el-table-column label="类型" align="center" :formatter="typeFormat" />
          <el-table-column label="明细类型" align="center" :formatter="mxTypeFormat" />
          <el-table-column label="备注" align="center" prop="remarks" />
          <el-table-column label="创建时间" align="center" prop="createTime" />
        </el-table>
        <pagination v-show="total2 > 0" :total="total2" :page.sync="queryParams3.pageNum"
          @pagination="balanceMx(undefined)" />
      </el-row>
    </el-dialog>

    <el-dialog title="用户积分明细" :visible.sync="visible3" width="1000px" top="5vh" append-to-body>
      <Integral v-if="userId > 0" :userId="userId" />
    </el-dialog>

    <el-dialog title="用户收货地址" :visible.sync="visible4" width="1300px" top="5vh" append-to-body>
      <Address v-if="userId > 0" :userId="userId" />
    </el-dialog>
    <el-dialog title="会员信息" :visible.sync="visible6" width="500px" top="5vh" append-to-body>
      <el-descriptions border :column="1">
        <el-descriptions-item label="用户聊天权益">{{ userMember.b }}次</el-descriptions-item>
        <el-descriptions-item label="查看微信权益">{{ userMember.c }}次</el-descriptions-item>
        <el-descriptions-item label="活动参加权益">{{ userMember.a }}次</el-descriptions-item>
      </el-descriptions>
    </el-dialog>


    <el-dialog title="认证资料" :visible.sync="visible7" width="1300px" top="5vh" append-to-body>
      <el-descriptions border :column="2">
        <el-descriptions-item label="身份证认证">
          <span v-if="userAuth.certStatus == 0">未认证</span>
          <span v-if="userAuth.certStatus == 1">已认证</span>
          <span v-if="userAuth.certStatus == 2">审核未通过</span>
        </el-descriptions-item>
        <el-descriptions-item label="身份证照片">
          <span v-if="userAuth.certPath"><el-image :src="userAuth.certPath.split(',')[0]"
              :preview-src-list="userAuth.certPath.split(',')" /></span>
          <span v-if="userAuth.certPath"><el-image :src="userAuth.certPath.split(',')[1]"
              :preview-src-list="userAuth.certPath.split(',')" /></span>
        </el-descriptions-item>
        <el-descriptions-item label="工作证认证">
          <span v-if="userAuth.workStatus == 0">未认证</span>
          <span v-if="userAuth.workStatus == 1">已认证</span>
          <span v-if="userAuth.workStatus == 2">审核未通过</span>
        </el-descriptions-item>
        <el-descriptions-item label="工作证照片">
          <span v-if="userAuth.workPath"><el-image :src="userAuth.workPath"
              :preview-src-list="[userAuth.workPath]" /></span>
        </el-descriptions-item>
        <el-descriptions-item label="学历证认证">
          <span v-if="userAuth.educationStatus == 0">未认证</span>
          <span v-if="userAuth.educationStatus == 1">已认证</span>
          <span v-if="userAuth.educationStatus == 2">审核未通过</span>
        </el-descriptions-item>
        <el-descriptions-item label="学历证照片">
          <span v-if="userAuth.educationPath"><el-image :src="userAuth.workPath"
              :preview-src-list="[userAuth.workPath]" /></span>
        </el-descriptions-item>
        <el-descriptions-item label="人脸核验">
          <span v-if="userAuth.isFacePass == 0">未认证</span>
          <span v-if="userAuth.isFacePass == 1">已认证</span>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <el-dialog title="用户详细信息" :visible.sync="visible5" width="1300px" top="5vh" append-to-body>
      <el-descriptions border :column="4">
        <el-descriptions-item label="微信昵称">{{ userDetail.nickName }}</el-descriptions-item>
        <el-descriptions-item label="性别">{{ genderFormat(userDetail) }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ userDetail.phone }}</el-descriptions-item>
        <el-descriptions-item label="出生日期">{{ userDetail.birthday }}</el-descriptions-item>
        <el-descriptions-item label="星座">{{ userDetail.constellation }}</el-descriptions-item>
        <el-descriptions-item label="身高">{{ userDetail.height }}</el-descriptions-item>
        <el-descriptions-item label="年收入">{{ userDetail.income }}</el-descriptions-item>
        <el-descriptions-item label="工作生活">{{ userDetail.habit }}</el-descriptions-item>
        <el-descriptions-item label="体重">{{ userDetail.weight }}</el-descriptions-item>
        <el-descriptions-item label="学历">{{ userDetail.education }}</el-descriptions-item>
        <el-descriptions-item label="微信号">{{ userDetail.wechatNumber }}</el-descriptions-item>
        <el-descriptions-item label="民族">{{ userDetail.people }}</el-descriptions-item>
        <el-descriptions-item label="户籍">{{ userDetail.birthPlace }}</el-descriptions-item>
        <el-descriptions-item label="婚姻状况"><span v-if="userDetail.marriage == 0">未婚</span><span
            v-if="userDetail.marriage == 1">离异</span></el-descriptions-item>
        <el-descriptions-item label="职业">{{ userDetail.occupation }}</el-descriptions-item>
        <el-descriptions-item label="信仰">{{ userDetail.religion }}</el-descriptions-item>
        <el-descriptions-item label="饮酒"><span>{{ userDetail.alcohol }}</span></el-descriptions-item>
        <el-descriptions-item label="吸烟"><span>{{ userDetail.cigar }}</span></el-descriptions-item>
        <el-descriptions-item label="家中排行">{{ userDetail.familyOrder }}</el-descriptions-item>
        <el-descriptions-item label="有无子女"><span v-if="userDetail.child == 0">无</span><span
            v-if="userDetail.child == 1">有</span></el-descriptions-item>
      </el-descriptions>
    </el-dialog>

  </div>
</template>

<script>
import { getAll } from "@/api/wx/auth";
import { listYppUser } from "@/api/ypp/user";
import { listUser, getUser, delUser, addUser, updateUser, exportUser, findFriends, getDetail } from "@/api/wx/user";
import { listMx } from "@/api/wx/balanceMx";
import { checkPermi, checkRole } from "@/utils/permission.js";
import { listClient } from "@/api/wx/client";
import Integral from "../integral";
import Address from "../address";
import { getQy } from "@/api/wx/qy";

export default {
  name: "User",
  components: { Integral, Address },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 遮罩层
      visible2: false,

      visible3: false,
      visible4: false,
      //用户详情
      visible5: false,
      //用户择偶标准
      visible6: false,
      //用户个人认证
      visible7: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 微信好友总条数
      total2: 0,
      // 余额明细总条数
      total3: 0,
      // 微信用户表格数据
      userList: [],
      // 微信好友用户表格数据
      dbTableList: [],
      // 余额明细表格数据
      mxTableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 性别字典
      genderStatus: [],
      // 余额类型
      typeOptions: [],
      // 余额明细类型
      mxTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        headUrl: undefined,
        openid: undefined,
        phone: undefined,
        status: undefined,
        clientId: undefined
      },
      // 表单参数
      form: {},
      // 微信好友查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        parentId: undefined,
        phone: undefined,
        nickName: undefined
      },
      // 余额明细查询参数
      queryParams3: {
        pageNum: 1,
        pageSize: 10,
        wxUserId: undefined
      },
      // 表单校验
      rules: {
      },
      clientList: [],
      userId: undefined,
      userDetail: {},
      userMember: {},
      userAuth: {},
    };
  },
  created() {
    // 性别字典翻译
    this.getDicts("wx_user_sex").then(response => {
      this.genderStatus = response.data;
    });
    // 用户状态字典翻译
    this.getDicts("wx_user_status").then(response => {
      this.statusOptions = response.data;
    });
    // 余额字典翻译
    this.getDicts("wx_balance_type").then(response => {
      this.typeOptions = response.data;
    });
    // 余额类型字典字典翻译
    this.getDicts("wx_balance_mx_type").then(response => {
      this.mxTypeOptions = response.data;
    });
    this.getList();

    listClient().then(response => {
      this.clientList = response.rows;
    });
  },
  methods: {
    /** 查询微信用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCheckPermi(v) {
      return checkPermi([v]);
    },
    handleCheckRole(v) {
      return checkRole([v]);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 性别字典翻译
    genderFormat(row) {
      return this.selectDictLabel(this.genderStatus, row.gender);
    },
    // 状态字典翻译
    statusFormat(row) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 余额类型字典翻译
    typeFormat(row) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 余额明细类型字典翻译
    mxTypeFormat(row) {
      return this.selectDictLabel(this.mxTypeOptions, row.mxType);
    },
    clientFormat(row) {
      let txt = '';
      this.clientList.filter(e => {
        if (e.id == row.clientId) {
          txt = e.name;
          return;
        }
      })
      return txt;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nickName: undefined,
        headUrl: undefined,
        openid: undefined,
        phone: undefined,
        delFlag: undefined,
        createTime: undefined,
        updateTime: undefined,
        status: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery2() {
      this.queryParams2.pageNum = 1;
      this.findFriends(undefined);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetQuery2() {
      this.queryParams2.nickName = undefined;
      this.queryParams2.phone = undefined;
      this.findFriends(undefined);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信用户";
    },
    //查看详细信息
    checkDetail(row) {
      const ids = row.id || this.ids[0];
      this.userDetail = {};
      this.visible5 = true;
      getDetail(ids).then(res => {
        this.userDetail = res.data
      })
    },
    //查看会员信息
    checkMember(row) {
      const ids = row.id || this.ids[0];
      this.visible6 = true;
      this.userMember = {};
      getQy(ids).then(res => {
        if (res.code == 200) {
          if (res.data) {
            this.userMember = res.data
          } else {
            this.userMember = {
              a: 0,
              b: 0,
              c: 0
            }
          }
        }

      })
    },
    //认证资料
    checkAuth(row) {
      const ids = row.id || this.ids[0];
      this.visible7 = true;
      this.userAuth = {}
      getAll(ids).then(res => {
        this.userAuth = res.data
      })
    },

    // 查询好友数量
    findFriends(row) {
      console.log(row);
      const ids = row.id || this.ids[0];
      this.queryParams2.parentId = ids;

      this.loading = false;
      findFriends(this.queryParams2).then(response => {
        this.dbTableList = response.rows;
        this.total2 = response.total;
        this.loading = false;
        this.visible = true;
      });
    },
    // 用户余额明细
    balanceMx(row) {
      const id = row.id || this.ids[0];
      this.queryParams3.wxUserId = id;

      this.loading = false;
      listMx(this.queryParams3).then(response => {
        this.mxTableList = response.rows;
        this.total3 = response.total;
        this.loading = false;
        this.visible2 = true;
      });
    },
    // 积分明细
    integralMx() {
      this.visible3 = true;
      console.log(this.ids[0]);

      this.userId = this.ids[0];
    },
    userAddress() {
      this.visible4 = true;
      this.userId = this.ids[0];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微信用户";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUser(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addUser(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除微信用户编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有微信用户数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportUser(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () { });
    }
  }
};
</script>
